/*********************************************************************************/
/*                                                                               */
/*      MCU Driver                                                               */
/*                                                                               */
/*      Last updated by:  CBS                                                    */
/*      Last update date: 13/01/25                                               */
/*      Revision:         0                                                      */
/*      Copyright:        DENSO                                                  */
/*                                                                               */
/*********************************************************************************/

/*********************************************************************************/
/*  include files                                                                */
/*********************************************************************************/
#include "MPC5674F_MVx264.h"
#include "Mcu.h"

/*********************************************************************************/
/*  prototypes                                                                   */
/*********************************************************************************/
static Std_ReturnType Mcu_CheckInit(const Mcu_ConfigType* ConfigPtr);
static Std_ReturnType Mcu_CheckInitClock(Mcu_ClockType ClockSetting);
static Std_ReturnType Mcu_CheckGetVersionInfo(Std_VersionInfoType* versioninfo);
void Det_ReportError(U2 a, U1 b, U1 c, U1 d);

/*********************************************************************************/
/*  defines / data types / structs / unions / macros                             */
/*********************************************************************************/

/*********************************************************************************/
/*  constants                                                                    */
/*********************************************************************************/
/* Pointer to configuration structure */
static const Mcu_ConfigType *McuConfigPtr;

/*********************************************************************************/
/*  variables                                                                    */
/*********************************************************************************/
typedef enum
{
    MCU_UNINIT,
    MCU_INIT
}Mcu_StateType;

#if (MCU_VERSION_INFO_API == STD_ON)
static Std_VersionInfoType MCU_VersionInfo =
{
    MCU_VENDOR_ID, 
    MODULE_ID_MCU,
    MCU_INSTANCE_ID,
    MCU_SW_MAJOR_VERSION,
    MCU_SW_MINOR_VERSION,
    MCU_SW_PATCH_VERSION,
    MCU_AR_MAJOR_VERSION,
    MCU_AR_MINOR_VERSION,
    MCU_AR_PATCH_VERSION 
};
#endif  /* MCU_VERSION_INFO_API */

static Mcu_StateType mcuState = MCU_UNINIT;

/*********************************************************************************/
/*  API Functions                                                                */
/*********************************************************************************/

/***************************************************************************************************/
/*  Service name    :   Mcu_Init                                                                   */
/*  Sync/Async      :   Synchronous                                                                */
/*  Reentrancy      :   Non Reentrant                                                              */
/*  Parameters (in) :   Pointer to MCU driver configuration.                                       */
/*  Return value    :   None                                                                       */
/*  Discription     :   This service initializes the MCU driver.                                   */
/***************************************************************************************************/
void Mcu_Init(const Mcu_ConfigType* ConfigPtr)
{
    if(E_OK == Mcu_CheckInit(ConfigPtr))
    {
        McuConfigPtr = ConfigPtr;

        FMPLL.ESYNCR2.B.LOLRE = 0;
        FMPLL.ESYNCR2.B.LOCRE = 0;
        FMPLL.ESYNCR2.B.LOLIRQ = 0;
        FMPLL.ESYNCR2.B.LOCIRQ = 0;

        mcuState = MCU_INIT;
    }

}

/***************************************************************************************************/
/*  Service name    :   Mcu_InitClock                                                              */
/*  Sync/Async      :   Synchronous                                                                */
/*  Reentrancy      :   Non Reentrant                                                              */
/*  Parameters (in) :   ClockSetting      Clock Setting                                            */
/*  Return value    :   Std_ReturnTpye    E_OK: command has been accepted.                         */
/*                                        E_NOT_OK: command has not been accepted.                 */
/*  Discription     :   This service initializes the PLL and other MCU specific clock options.     */
/***************************************************************************************************/
#if(MCU_INIT_CLOCK == STD_ON)
Std_ReturnType Mcu_InitClock(Mcu_ClockType ClockSetting)
{
    Std_ReturnType returnValue;

    if(E_OK == Mcu_CheckInitClock(ClockSetting)) 
    {
        FMPLL.ESYNCR2.B.ERFD = (McuConfigPtr->McuClockSettingConfig[ClockSetting].McuEnhancedReducedFrequencyDivider) + 2;
        FMPLL.ESYNCR1.B.EPREDIV = McuConfigPtr->McuClockSettingConfig[ClockSetting].McuEnhancedPreDivider;
        FMPLL.ESYNCR1.B.EMFD = McuConfigPtr->McuClockSettingConfig[ClockSetting].McuEnhancedMultiplicationFactorDivider;
        while(!FMPLL.SYNSR.B.LOCK)
        {
            ;
        }
        FMPLL.ESYNCR2.B.ERFD = McuConfigPtr->McuClockSettingConfig[ClockSetting].McuEnhancedReducedFrequencyDivider;

        returnValue = E_OK;
    }
    else
    {
        returnValue = E_NOT_OK;  /* An error has been raised from Mcu_InitClock */
    }
    return(returnValue);
}
#endif  /* MCU_INIT_CLOCK */

/***************************************************************************************************/
/*  Service name    :   Mcu_GetPllStatus                                                           */
/*  Sync/Async      :   Synchronous                                                                */
/*  Reentrancy      :   Reentrant                                                                  */
/*  Parameters (in) :   None                                                                       */
/*  Return value    :   Mcu_PllStatusType    PLL Status                                            */
/*  Discription     :   This service provides the lock status of the PLL.                          */
/***************************************************************************************************/
Mcu_PllStatusType Mcu_GetPllStatus(void)
{
    Mcu_PllStatusType returnValue;
#if(MCU_NO_PLL == FALSE)
    if(mcuState == MCU_UNINIT)
    {
        returnValue = MCU_PLL_STATUS_UNDEFINED;
    }
    else
    {
        if(FMPLL.SYNSR.B.LOCK == 0)
        {
            returnValue = MCU_PLL_UNLOCKED;
        }
        else
        {
            returnValue = MCU_PLL_LOCKED;
        }
    }
#else
    returnValue = MCU_PLL_STATUS_UNDEFINED;
#endif  /* MCU_NO_PLL */
    return returnValue;
}

/********************************************************************************************************/
/*  Service name    :   Mcu_GetVersionInfo                                                              */
/*  Sync/Async      :   Synchronous                                                                     */
/*  Reentrancy      :   Reentrant                                                                       */
/*  Parameters (in) :   versioninfo   Pointer to where to store the version information of this module. */
/*  Return value    :   None                                                                            */
/*  Discription     :   This service returns the version information of this module.                    */
/********************************************************************************************************/
#if(MCU_VERSION_INFO_API == STD_ON)  /* MCU104 */
void Mcu_GetVersionInfo(Std_VersionInfoType* versioninfo)
{
    if(E_OK == Mcu_CheckGetVersionInfo(versioninfo))
    {
        versioninfo->vendorID = MCU_VersionInfo.vendorID;
        versioninfo->moduleID = MCU_VersionInfo.moduleID;
        versioninfo->instanceID = MCU_VersionInfo.instanceID;

        versioninfo->sw_major_version = MCU_VersionInfo.sw_major_version;
        versioninfo->sw_minor_version = MCU_VersionInfo.sw_minor_version;
        versioninfo->sw_patch_version = MCU_VersionInfo.sw_patch_version;

        versioninfo->ar_major_version = MCU_VersionInfo.ar_major_version;
        versioninfo->ar_minor_version = MCU_VersionInfo.ar_minor_version;
        versioninfo->ar_patch_version = MCU_VersionInfo.ar_patch_version;
    }
}
#endif  /* MCU_VERSION_INFO_API */

/*********************************************************************************/
/*    Local Functions                                                            */
/*********************************************************************************/
static Std_ReturnType Mcu_CheckInit(const Mcu_ConfigType* ConfigPtr)
{
    Std_ReturnType returnValue;

#if(MCU_DEV_ERROR_DETECT == STD_ON)
    if(ConfigPtr == (Mcu_ConfigType*)0)
    {
        Det_ReportError(MODULE_ID_MCU,MCU_INSTANCE_ID,MCU_INIT_SERVICE_ID,MCU_E_PARAM_CONFIG);
        returnValue = E_NOT_OK;
    }
    else
    {
        returnValue = E_OK;
    }
#else
    returnValue = E_OK;
#endif  /* MCU_DEV_ERROR_DETECT */
    return(returnValue);
}

#if(MCU_INIT_CLOCK == STD_ON)
static Std_ReturnType Mcu_CheckInitClock(Mcu_ClockType ClockSetting)
{
    Std_ReturnType returnValue;

#if(MCU_DEV_ERROR_DETECT == STD_ON)
    if(MCU_UNINIT == mcuState)
    {
        /* MCU139 */
        Det_ReportError(MODULE_ID_MCU,MCU_INSTANCE_ID,MCU_INITCLOCK_SERVICE_ID,MCU_E_UNINIT);
        returnValue = E_NOT_OK;
    }
    else if(!(ClockSetting < McuConfigPtr->McuClockSettings))
    {
        Det_ReportError(MODULE_ID_MCU,MCU_INSTANCE_ID,MCU_INITCLOCK_SERVICE_ID,MCU_E_PARAM_CLOCK);
        returnValue = E_NOT_OK;
    }
    else
    {
        returnValue = E_OK;
    }
#else
    returnValue = E_OK;
#endif  /* MCU_DEV_ERROR_DETECT */
    return(returnValue);
}
#endif  /* MCU_INIT_CLOCK */

#if (MCU_VERSION_INFO_API == STD_ON)  /* MCU104 */
static Std_ReturnType Mcu_CheckGetVersionInfo(Std_VersionInfoType* versioninfo)
{
    Std_ReturnType returnValue;

#if( MCU_DEV_ERROR_DETECT == STD_ON )
    if( 0 == versioninfo)
    {
        /* MCU204 */
        Det_ReportError(MODULE_ID_MCU,MCU_INSTANCE_ID,MCU_GETVERSIONINFO_SERVICE_ID,MCU_E_PARAM_POINTER );
        returnValue = E_NOT_OK;
    }
    else
    {
        returnValue = E_OK;
    }
#else
    returnValue = E_OK;
#endif  /* MCU_DEV_ERROR_DETECT */
    return(returnValue);
}
#endif  /* MCU_VERSION_INFO_API */

void Det_ReportError(U2 a, U1 b, U1 c, U1 d)
{
    
}

/**** End Of File ****************************************************************/
